#include "INCLUDE/SvgChild.h"

#include <QMouseEvent>
#include <QSvgRenderer>
#include "../JWTS/INCLUDE/AnsiConverter.h"

SvgChild::SvgChild(const char *p_name, const char *p_file_name, QWidget* parent) :
    QSvgWidget(toQtStr(p_file_name), parent),
    MdiChild(p_name)
{
    setAttribute(Qt::WA_DeleteOnClose);
}

void SvgChild::closeEvent(QCloseEvent *event)
{
    emit pSignalEmitter()->aboutToClose(this);

    QSvgWidget::closeEvent(event);
}

void SvgChild::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        auto svgSize = renderer()->defaultSize();
        auto s = size();
        auto new_x = (s.height() * svgSize.width())/svgSize.height();
        auto new_y = (s.width() * svgSize.height())/svgSize.width();
        auto ds = parentWidget()->size() - s;

        if (new_x > s.width())
            parentWidget()->resize(new_x + ds.width(), s.height() + ds.height());
        else if (new_y > s.height())
            parentWidget()->resize(s.width() + ds.width(), new_y + ds.height());
    }
    QSvgWidget::mouseDoubleClickEvent(event);
}
